<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class admin_news_model extends CI_Model
{

    public function __construct()
    {
        // Call the Model constructor
        parent::__construct();        
    }     

    function get_count_news($type,$status = null)
    {    
        $this->db->select('count(*) as news_count')->from('landing_post');
    	if($status){
        	$this->db->where('status',$status);        	
        }
        $this->db->where('type',$type);
        $rs = $this->db->get();
    	if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();            
            $rs->free_result();
            return $return[0]['news_count'];                        
        }
    }
    
    function get_all_news($start, $limit, $type, $status = null)
    {    	
        $this->db->select('*');
        $this->db->from('landing_post');
        if($status){
        	$this->db->where('status',$status);        	
        }         
        $this->db->where('type',$type);      
        $this->db->limit($limit,$start);
        $rs = $this->db->get();
    	if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();            
            $rs->free_result();
            return $return;                        
        }               
               
    }  
       
    function get_news_by_id($id)
    {
    	$this->db->select('*')->from('landing_post')->where('id',$id)->where('type',1);
    	$rs = $this->db->get();
    	if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();            
            $rs->free_result();            
            return $return[0];
        }
    }
    
    function add_news($data){     	
    	return ($this->db->insert('landing_post', $data)) ? $this->db->insert_id() : false;
    }       
    
    function update_news($data){    	
    	$this->db->where('id', $data['id']);
    	return $this->db->update('landing_post',$data);
    }
    
    function delete_news($id){
   		return $this->db->delete('landing_post',array('id' => $id));
    }
}